//
//  ViewController.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic,strong) NSMutableArray *timelist;
@end

@implementation ViewController


- (NSMutableArray *)timelist
{
    if(_timelist == nil)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self listPath]])
        {
            self.timelist = [NSMutableArray arrayWithContentsOfFile:[self listPath]];
        }
        else
        {
            _timelist = [NSMutableArray array];
        }
    }
    return _timelist;
}
             
- (NSString *)listPath
 {
     NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
     NSString *filePath = [docPath stringByAppendingPathComponent:@"datas.plist"];
     return filePath;
 }


- (void)savePlist
{
    [self.timelist writeToFile:[self listPath] atomically:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
//    * 将取消活动    WillResignActive
//    * 已经进入后台  DidEnterBackground
//    * 将进入前台    WillEnterForeground
//    * 已经激活      DidBecomeActive
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo1) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo2) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo3) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo4) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    float redcolor=[d floatForKey:@"red"]/255.0;
    float bluecolor=[d floatForKey:@"blue"]/255.0;
    float greeencolor=[d floatForKey:@"green"]/255.0;
    self.view.backgroundColor=[UIColor colorWithRed:redcolor green:greeencolor blue:bluecolor alpha:255/255.0];
    
}

- (void)reload
{
    NSMutableString *text = [[NSMutableString alloc] init];
    for (long int i=0; i<self.timelist.count; i++)
    {
        [text insertString:[NSString stringWithFormat:@"%@\n",self.timelist[i]] atIndex:0];
    }
    self.textView.text = [NSString stringWithFormat:@"%@",text];
}

- (void)updateInfo1
{
    NSDate *now = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"HH:mm:ss"];
    NSString *tmp = [df stringFromDate:now];
    NSString *n = [NSString stringWithFormat:@"%@ 将取消活动",tmp];
    [self.timelist addObject:n];
    [self savePlist];
    [self reload];
}

- (void)updateInfo2
{
    NSDate *now = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"HH:mm:ss"];
    NSString *tmp = [df stringFromDate:now];
    NSString *n = [NSString stringWithFormat:@"%@ 已经进入后台",tmp];
    [self.timelist addObject:n];
    [self savePlist];
    [self reload];
}

- (void)updateInfo3
{
    NSDate *now = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"HH:mm:ss"];
    NSString *tmp = [df stringFromDate:now];
    NSString *n = [NSString stringWithFormat:@"%@ 将进入前台",tmp];
    [self.timelist addObject:n];
    [self savePlist];
    [self reload];
}

- (void)updateInfo4
{
    NSDate *now = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"HH:mm:ss"];
    NSString *tmp = [df stringFromDate:now];
    NSString *n = [NSString stringWithFormat:@"%@ 已经激活",tmp];
    [self.timelist addObject:n];
    [self savePlist];
    [self reload];
}



@end
